package com.sll.hosptials.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sll.hosptials.controller.req.*;
import com.sll.hosptials.controller.resp.RoleInfoDetailResp;
import com.sll.hosptials.model.RoleInfo;
import com.sll.hosptials.model.UserInfo;
import com.sll.hosptials.service.RoleInfoService;
import com.sll.hosptials.mapper.RoleInfoMapper;
import com.sll.hosptials.vo.ServiceException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

import static com.sll.hosptials.config.ParamConfig.ENABLE;

/**
 * @author shaolulu
 * @description 针对表【role_info(角色信息)】的数据库操作Service实现
 * @createDate 2024-01-02 14:21:56
 */
@Service
public class RoleInfoServiceImpl extends ServiceImpl<RoleInfoMapper, RoleInfo> implements RoleInfoService {

    @Override
    public Boolean addOrUpdateRoleInfo(AddOrUpdateRoleInfoReq req) {
        LambdaQueryWrapper<RoleInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleInfo::getRoleName, req.getRoleName())
                .ne(ObjectUtil.isNotEmpty(req.getRoleId()), RoleInfo::getRoleId, req.getRoleId())
                .eq(RoleInfo::getEnable, ENABLE);
        if (Optional.ofNullable(this.getOne(queryWrapper)).isPresent()) {
            throw new ServiceException("角色名:" + req.getRoleName() + "已存在，不可重复");
        }
        return this.saveOrUpdate(BeanUtil.copyProperties(req, RoleInfo.class));
    }

    @Override
    public Page<RoleInfoDetailResp> rolePage(RoleInfoPageReq req) {
        LambdaQueryWrapper<RoleInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(req.getRoleName()), RoleInfo::getRoleName, req.getRoleName());
        queryWrapper.like(StrUtil.isNotEmpty(req.getRoleCode()), RoleInfo::getRoleCode, req.getRoleCode());
        queryWrapper.orderByDesc(RoleInfo::getCreateDateTime);
        IPage<RoleInfoDetailResp> pageRespIPage = this.page(new Page<>(req.getPageNo(), req.getPageSize()), queryWrapper)
                .convert(info -> {
                    return BeanUtil.copyProperties(info, RoleInfoDetailResp.class);
                });
        return (Page<RoleInfoDetailResp>) pageRespIPage;
    }

    @Override
    public Boolean delete(IdReq req) {
        if (ObjectUtil.isNotEmpty(req.getId())) {
            return this.removeById(req.getId());
        }
        if (CollUtil.isNotEmpty(req.getIds())) {
            return this.removeByIds(req.getIds());
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean bindUserRole(UserBindRoleReq req) {

        return null;
    }

    @Override
    public List<RoleInfoDetailResp> getRoleInfoList(RoleInfoListReq req) {
        LambdaQueryWrapper<RoleInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleInfo::getEnable, ENABLE);
        return BeanUtil.copyToList(this.list(queryWrapper), RoleInfoDetailResp.class);
    }

    @Override
    public RoleInfoDetailResp detail(IdReq req) {
        if (ObjectUtil.isNotEmpty(req.getId())) {
            RoleInfo roleInfo = this.getById(req.getId());
            return BeanUtil.copyProperties(roleInfo, RoleInfoDetailResp.class);
        }

        return null;
    }
}




